package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangxiang.approval.common.IntegerCommon;
import com.hangxiang.approval.common.UserManagerInsertCommon;
import com.hangxiang.approval.common.UserManagerUpdateCommon;
import com.hangxiang.approval.dao.UserManagerDao;
import com.hangxiang.approval.entity.UserManager;
import com.hangxiang.approval.enums.ResultStatusEnum;
import com.hangxiang.approval.exception.MyException;
import com.hangxiang.approval.service.UserManagerService;
import com.hangxiang.approval.vo.UserManagerVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * (UserManager)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 13:59:13
 */
@Service("userManagerService")
public class UserManagerServiceImpl extends ServiceImpl<UserManagerDao, UserManager> implements UserManagerService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private UserManagerDao userManagerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public UserManagerVo queryById(IntegerCommon common) {
        UserManagerVo vo = new UserManagerVo();
        UserManager userManager = userManagerDao.selectById(common.getValue());
        BeanUtils.copyProperties(userManager, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param userManagerInsertCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer insertUserManager(UserManagerInsertCommon userManagerInsertCommon) {
        UserManager user=userManagerDao.selectByCommon(userManagerInsertCommon);
        if (user==null){
            UserManager userManager = new UserManager();
            BeanUtils.copyProperties(userManagerInsertCommon, userManager);
            return userManagerDao.insert(userManager);
        }else {
            throw new MyException(ResultStatusEnum.MANAGER);
        }
    }

    /**
     * 修改数据
     *
     * @param userManagerUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer updateUserManager(UserManagerUpdateCommon userManagerUpdateCommon) {
        UserManager userManager = new UserManager();
        BeanUtils.copyProperties(userManagerUpdateCommon, userManager);
        return userManagerDao.updateById(userManager);
    }
}